<!--

    Copyright © 2016-2020 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="keyFilterFormGroup" (ngSubmit)="save()" style="width: 900px;">
  <mat-toolbar color="primary">
    <h2>{{(data.isAdd ? 'filter.add-key-filter' : (data.readonly ? 'filter.key-filter' : 'filter.edit-key-filter')) | translate}}</h2>
    <span fxFlex></span>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content>
    <fieldset [disabled]="isLoading$ | async" fxLayout="column">
      <section fxLayout="row" fxLayoutGap="8px" class="entity-key">
        <section fxFlex="70" fxLayout="row" formGroupName="key" fxLayoutGap="8px">
          <mat-form-field fxFlex="40" class="mat-block">
            <mat-label translate>filter.key-type.key-type</mat-label>
            <mat-select required formControlName="type">
              <mat-option *ngFor="let type of entityKeyTypes" [value]="type">
                {{entityKeyTypeTranslations.get(type) | translate}}
              </mat-option>
            </mat-select>
          </mat-form-field>
          <mat-form-field fxFlex="60" class="mat-block">
            <mat-label translate>filter.key-name</mat-label>
            <input matInput required formControlName="key"
                   [matAutocomplete]="auto"
                   [matAutocompleteDisabled]="keyFilterFormGroup.get('key.type').value !== entityField">
            <mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
              <mat-option *ngFor="let option of filteredEntityFields | async" [value]="option">
                {{option}}
              </mat-option>
            </mat-autocomplete>
            <mat-error *ngIf="keyFilterFormGroup.get('key.key').hasError('required')">
              {{ 'filter.key-name-required' | translate }}
            </mat-error>
          </mat-form-field>
        </section>
        <mat-form-field fxFlex="30" class="mat-block">
          <mat-label translate>filter.value-type.value-type</mat-label>
          <mat-select formControlName="valueType">
            <mat-select-trigger>
              <mat-icon class="tb-mat-18" svgIcon="{{ entityKeyValueTypes.get(keyFilterFormGroup.get('valueType').value)?.icon }}"></mat-icon>
              <span>{{ entityKeyValueTypes.get(keyFilterFormGroup.get('valueType').value)?.name | translate }}</span>
            </mat-select-trigger>
            <mat-option *ngFor="let valueType of entityKeyValueTypesKeys" [value]="valueType">
              <mat-icon class="tb-mat-18" svgIcon="{{ entityKeyValueTypes.get(entityKeyValueTypeEnum[valueType]).icon }}"></mat-icon>
              <span>{{ entityKeyValueTypes.get(entityKeyValueTypeEnum[valueType]).name | translate }}</span>
            </mat-option>
          </mat-select>
          <mat-error *ngIf="keyFilterFormGroup.get('valueType').hasError('required')">
            {{ 'filter.value-type-required' | translate }}
          </mat-error>
        </mat-form-field>
      </section>
      <tb-filter-predicate-list *ngIf="keyFilterFormGroup.get('valueType').value"
                                [allowUserDynamicSource]="data.allowUserDynamicSource"
                                [displayUserParameters]="data.displayUserParameters"
                                [valueType]="keyFilterFormGroup.get('valueType').value"
                                [key]="keyFilterFormGroup.get('key.key').value"
                                formControlName="predicates">
      </tb-filter-predicate-list>
    </fieldset>
  </div>
  <div mat-dialog-actions fxLayoutAlign="end center">
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            (click)="cancel()"
            cdkFocusInitial>
      {{ (data.readonly ? 'action.close' : 'action.cancel') | translate }}
    </button>
    <button mat-raised-button color="primary"
            type="submit"
            *ngIf="!data.readonly"
            [disabled]="(isLoading$ | async) || keyFilterFormGroup.invalid || !keyFilterFormGroup.dirty">
      {{ (data.isAdd ? 'action.add' : 'action.update') | translate }}
    </button>
  </div>
</form>
